import { GraphQLModule, registerEnumType } from '@nestjs/graphql';
import { ApolloDriver, ApolloDriverConfig } from '@nestjs/apollo';
import {
  Logger,
  MiddlewareConsumer,
  Module,
  NestModule,
  OnApplicationBootstrap,
  OnApplicationShutdown,
} from '@nestjs/common';
import { Prisma } from '@sker/prisma';
import { SkerNestMiddleware } from './sker.interceptor';
import { GraphqlModule } from './graphql.module';
import { AppController } from './app.controller';
import { readdirSync } from 'fs-extra';
import { join } from 'path';
import { container } from 'tsyringe';
import { ROOT } from '@sker/core';
registerEnumType(Prisma.SortOrder, {
  name: 'SortOrder',
});
@Module({
  imports: [
    GraphQLModule.forRoot<ApolloDriverConfig>({
      driver: ApolloDriver,
      transformAutoSchemaFile: true,
      autoSchemaFile: './schema.graphql',
      playground: true,
      resolvers: {},
    }),
    GraphqlModule
  ],
  controllers: [
    AppController
  ],
  providers: [],
})
export class AppModule
  implements OnApplicationBootstrap, OnApplicationShutdown, NestModule {
  constructor(

  ) { }
  configure(consumer: MiddlewareConsumer) {
    const root = container.resolve(ROOT)
    const addons: string[] = readdirSync(join(root, 'addons'))
    const list = addons.filter(it => it.startsWith('@')).map(it => it.replace('@', ''))
    consumer
      .apply(SkerNestMiddleware)
      .forRoutes(...list);
  }
  async onApplicationBootstrap() {
    Logger.log(`app bootstrap`);
  }
  onApplicationShutdown(signal?: string) {
    Logger.log(`app shutdown`, signal);
  }
}
